// @Author EthanScriptOn
// @Desc
package background

import (
	switchconfig "gitee.com/fatzeng/srf_switch/config"
	"gitee.com/fatzeng/srf_switch_basic_components/common"
	"gitee.com/fatzeng/srf_switch_basic_components/config"
	"gitee.com/fatzeng/srf_switch_basic_components/parser"
	"gitee.com/fatzeng/srf_switch_basic_components/switch_log"
	"gitee.com/fatzeng/srf_switch_basic_components/switch_log/center"
	"gitee.com/fatzeng/srf_switch_basic_components/util"
)

var FilePathBackground = "./resource/"

func main() {
	ctx, err := GenerateSrfContext()
	if err != nil {
		panic(err)
	}
	err = util.FunWithSync(func() error {
		return config.ConfigOperateInitialization(ctx,
			parser.GenerateFileInformation(ctx.GetFilePath()),
			parser.GenerateArgsInformation(ctx.GetCommandArgs()))
	}, func() (err error) {
		profiles := GenerateLogProfiles()
		ctx.SetLogProfiles(profiles)
		return switch_log.LogInitialization(ctx)
	}, func() error {
		return OperatorStart()
	})
	if err != nil {
		panic(err)
	}
}

func GenerateSrfContext() (ctx *common.SrfContext, err error) {
	srfCtx := new(common.SrfContext)
	srfCtx.SetConfigContext(parser.GenerateDefaultConfigureContext(true))
	srfCtx.SetFilePath(FilePathBackground)
	err = srfCtx.SetSrfSwitchConfig(switchconfig.SrfSwitchGlobalConfig)
	if err != nil {
		return nil, err
	}
	return srfCtx, nil
}

func GenerateLogProfiles() []*center.LogProfile {
	profiles := make([]*center.LogProfile, 0)
	for _, logConfig := range switchconfig.SrfSwitchGlobalConfig.SrfLogConfig {
		profile := new(center.LogProfile)
		profile.Template = logConfig.Template
		standards := make([]*center.LogFilterCriteria, 0)
		for _, filter := range logConfig.Filter {
			criteria := new(center.LogFilterCriteria)
			criteria.FilterTag = filter.FilterTag
			criteria.FilterEnable = filter.FilterEnable
			standards = append(standards, criteria)
		}
		profile.Filter = standards

		destinations := make([]*center.OutputDestination, 0)
		for _, outputConfig := range logConfig.Output {
			destination := new(center.OutputDestination)
			destination.OutputPath = outputConfig.OutputPath
			destination.OutputType = outputConfig.OutputType
			destinations = append(destinations)
		}
		profile.Output = destinations
	}
	return profiles
}
